package com.zym.init.core.validator;

import com.zym.init.core.annotation.FutureOrPresentDate;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.Date;

/**
 * 验证注解@FutureOrPresentDate是否为将来或现在的时间
 */
public class FutureOrPresentDateValidator implements ConstraintValidator<FutureOrPresentDate, Date> {
    @Override
    public boolean isValid(Date date, ConstraintValidatorContext context) {
        // 如果字段为空，则不进行验证
        if (date == null) {
            return true;
        }
        // 如果日期是将来或现在的时间，则返回true，否则返回false
        return !date.before(new Date());
    }
}
